package com.wolfeyes.framework.config;

import com.wolfeyes.framework.springsecurity.filter.CustomizeJwtAuthenticationLoginFilter;
import com.wolfeyes.framework.springsecurity.handle.CustomizeAuthenticationSuccessHandler;
import com.wolfeyes.framework.springsecurity.handle.CustomizeAuthenticationFailureHandler;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.authentication.dao.DaoAuthenticationProvider;
import org.springframework.security.config.annotation.SecurityConfigurerAdapter;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.security.web.DefaultSecurityFilterChain;
import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter;

/**
 * @description 登录过滤器的配置类
 * @see Talk is cheap, Show me the code. -- Linus Torvalds
 * @title 配置SpringSecurity安全框架
 * @author yanyljava
 * @create 2022-01-14 21:25
 */
@Configuration
public class JwtAuthenticationSecurityConfig extends SecurityConfigurerAdapter<DefaultSecurityFilterChain, HttpSecurity> {

	/**
	 * @title userDetailsService
	 */
	@Autowired
	@Qualifier("customizeUserDetailsService")
	private UserDetailsService userDetailsService;

	/**
	 * @title 登录成功处理器
	 */
	@Autowired
	private CustomizeAuthenticationSuccessHandler customizeAuthenticationSuccessHandler;

	/**
	 * @title 登录失败处理器
	 */
	@Autowired
	private CustomizeAuthenticationFailureHandler customizeAuthenticationFailureHandler;

	/**
	 * @title 加密
	 */
	@Autowired
	private PasswordEncoder passwordEncoder;

	/**
	 * @title AuthenticationManager 授权管理器
	 */
	@Autowired
	private AuthenticationManager authenticationManager;
	
	/**
	 * @title 将登录接口的过滤器配置到过滤器链中
	 * @title 1. 配置登录成功、失败处理器
	 * @title 2. 配置自定义的userDetailsService（从数据库中获取用户数据）
	 * @title 3. 将自定义的过滤器配置到spring security的过滤器链中，配置在UsernamePasswordAuthenticationFilter之前
	 * @param httpSecurity 核心安全配置
	 */
	@Override
	public void configure(HttpSecurity httpSecurity) {
		String defaultFilterProcessesUrl = "/sysUser/login";
		String httpMethod = "POST";
		CustomizeJwtAuthenticationLoginFilter customizeJwtAuthenticationLoginFilter = new CustomizeJwtAuthenticationLoginFilter(defaultFilterProcessesUrl, httpMethod);
		// 获取授权管理器
		AuthenticationManager authenticationManager = httpSecurity.getSharedObject(AuthenticationManager.class);
		customizeJwtAuthenticationLoginFilter.setAuthenticationManager(authenticationManager);
		//认证成功处理器
		customizeJwtAuthenticationLoginFilter.setAuthenticationSuccessHandler(customizeAuthenticationSuccessHandler);
		//认证失败处理器
		customizeJwtAuthenticationLoginFilter.setAuthenticationFailureHandler(customizeAuthenticationFailureHandler);
		//直接使用DaoAuthenticationProvider
		DaoAuthenticationProvider provider = new DaoAuthenticationProvider();
		//设置authenticationProvider
		httpSecurity.authenticationProvider(provider);
		//设置userDetailsService
		provider.setUserDetailsService(userDetailsService);
		//设置加密算法-BCryptPasswordEncoder
		provider.setPasswordEncoder(passwordEncoder);
		//将这个过滤器添加到UsernamePasswordAuthenticationFilter之前执行
		httpSecurity.addFilterBefore(customizeJwtAuthenticationLoginFilter, UsernamePasswordAuthenticationFilter.class);
	}
	
}


